package com.xszx.servlet.admin.admin;

import com.xszx.bean.Manager;
import com.xszx.dao.ManagerDao;
import com.xszx.dao.impl.ManagerDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddAdminServlet")
public class AddAdminServlet extends HttpServlet {
    static ManagerDao managerDao = new ManagerDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String managerName = req.getParameter("managerName");
        Integer role = Integer.parseInt(req.getParameter("role"));

        boolean flag = managerDao.addManager(new Manager(managerName, role));

        if (flag) {
            req.setAttribute("msg", "添加成功");
            resp.sendRedirect("GetAllAdminServlet");
        } else {
            req.setAttribute("msg", "用户名已存在");
            resp.sendRedirect("admin/addManager.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
